import datetime
import backtrader as bt
from strategies import *

# 初始化Cerebro引擎
cerebro = bt.Cerebro()

#设置数据参数并添加至Cerebro引擎
data = bt.feeds.YahooFinanceCSVData(
    dataname='TSLA.csv',
    fromdate=datetime.datetime(2016, 1, 1),
    todate=datetime.datetime(2017, 12, 25))
	# 样本外数据的设置
    #fromdate=datetime.datetime(2018, 1, 1),
    #todate=datetime.datetime(2019, 12, 25))

# 向Cerebro引擎添加数据
cerebro.adddata(data)

# 向Cerebro引擎添加策略
cerebro.addstrategy(MAcrossover)

# 头寸设置
cerebro.addsizer(bt.sizers.SizerFix, stake=3)

if __name__ == '__main__':

	start_portfolio_value = cerebro.broker.getvalue()

	# 运行Cerebro引擎
	cerebro.run()

	end_portfolio_value = cerebro.broker.getvalue()
	pnl = end_portfolio_value - start_portfolio_value
	print('Starting Portfolio Value: %.2f' % start_portfolio_value)
	print('Final Portfolio Value: %.2f' % end_portfolio_value)
	print('PnL: %.2f' % pnl)
